class IndexedDBManager {
    constructor(dbName, version, storeName) {
        this.dbName = dbName;
        this.version = version;
        this.storeName = storeName;
    }

    async openDB() {
        return new Promise((resolve, reject) => {
            let request = indexedDB.open(this.dbName, this.version);

            request.onerror = (event) => {
                reject(event);
            };

            request.onsuccess = (event) => {
                resolve(event.target.result);
            };

            request.onupgradeneeded = (event) => {
                let db = event.target.result;
                if (!db.objectStoreNames.contains(this.storeName)) {
                    db.createObjectStore(this.storeName, { keyPath: 'id' }); // 假设每个条目都有一个id字段作为主键
                }
            };
        });
    }

    async addData(data) {
        const db = await this.openDB();
        const transaction = db.transaction([this.storeName], 'readwrite');
        const store = transaction.objectStore(this.storeName);
        const request = store.add(data);
        return new Promise((resolve, reject) => {
            request.onsuccess = () => {
                resolve(data);
            };
            request.onerror = () => {
                reject('Data addition failed');
            };
        });
    }

    async getData(query) {
        const db = await this.openDB();
        const transaction = db.transaction([this.storeName], 'readonly');
        const store = transaction.objectStore(this.storeName);
        const request = store.get(query); // 假设我们通过id查询数据
        return new Promise((resolve, reject) => {
            request.onsuccess = () => {
                resolve(request.result);
            };
            request.onerror = () => {
                reject('Data retrieval failed');
            };
        });
    }

    async updateData(id, updatedData) { // 假设更新通过id进行，并且更新了整个条目对象的数据。
        const db = await this.openDB();
        const transaction = db.transaction([this.storeName], 'readwrite');
        const store = transaction.objectStore(this.storeName);
        const request = store.put(updatedData); // 使用put方法更新整个条目，如果id已存在则更新，否则添加新条目。
        return new Promise((resolve, reject) => {
            request.onsuccess = () => {
                resolve(updatedData);
            };
            request.onerror = () => {
                reject('Data update failed');
            };
        });
    }

    async deleteData(id) { // 假设通过id删除数据。也可以考虑使用范围删除等其他方式。
        const db = await this.openDB();
        const transaction = db.transaction([this.storeName], 'readwrite');
        const store = transaction.objectStore(this.storeName);
        const request = store.delete(id); // 根据id删除数据。也可以使用其他条件进行删除。例如store.delete(store.index('indexName').only(value))。 具体取决于你的需求。
        return new Promise((resolve, reject) => {
            request.onsuccess = () => {
                resolve(`Data with id ${id} deleted`); // 可以根据需要返回不同的
            }
        })

    }

    async clearStore() {
        const db = await this.openDB();
        const transaction = db.transaction([this.storeName], 'readwrite');
        const store = transaction.objectStore(this.storeName);
        store.clear();
        // db.createObjectStore(this.storeName, { keyPath: 'id' });

    }

}

export default IndexedDBManager